package com.gitee.wsl.doc.sample.rss.utils

import com.gitee.wsl.doc.sample.rss.ParseRSSKeyword
import com.gitee.wsl.doc.sample.rss.ParsingMode
import com.gitee.wsl.doc.sample.rss.RSSFeed
import com.gitee.wsl.doc.sample.rss.RSSVersion
import com.gitee.wsl.doc.sample.rss.RootDocument

object ParserExecutorUtils {

    private fun deduceDefaultNSParsingMode(root: com.gitee.wsl.doc.sample.rss.RSSFeed, element: com.gitee.wsl.doc.sample.rss.utils.ParseRSSElement) = when (element.name) {
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.RSSVersion.RSS_V2.elementName -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.RootDocument.RSS
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.CHANNEL -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Channel(root, false)
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.ITEM -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Item {
            root.items.add(it)
        }
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.IMAGE -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Image {
            root.image = it
        }
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.RSSVersion.RSS_ATOM.elementName -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.RootDocument.Atom + _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Channel(root, false)
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.ENTRY -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Item {
            root.items.add(it)
        }
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.AUTHOR -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Author()
        else -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Read()
    }

    private fun deduceRdfNSParsingMode(root: com.gitee.wsl.doc.sample.rss.RSSFeed, element: com.gitee.wsl.doc.sample.rss.utils.ParseRSSElement) = when (element.name) {
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.RSSVersion.RSS_V1.elementName -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.RootDocument.RDF
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.CHANNEL -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Channel(root, true)
        else -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Read()
    }

    private fun deduceMediaNSParsingMode(element: com.gitee.wsl.doc.sample.rss.utils.ParseRSSElement) = when (element.name) {
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.GROUP -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.MediaNS.Group()
        else -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Read()
    }

    /** deduce mode based on given name */
    fun deduceParsingMode(root: com.gitee.wsl.doc.sample.rss.RSSFeed, element: com.gitee.wsl.doc.sample.rss.utils.ParseRSSElement) = when (element.prefix) {
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.DEFAULT_NS -> deduceDefaultNSParsingMode(root, element)
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.RDF_NS -> deduceRdfNSParsingMode(root, element)
        _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParseRSSKeyword.MEDIA_NS -> deduceMediaNSParsingMode(element)
        else -> _root_ide_package_.com.gitee.wsl.doc.sample.rss.ParsingMode.Read()
    }
}
